package com.wuxiaoge.job;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class FirstJobDemo {

    @Bean
    public Job firstJob(JobRepository jobRepository, JdbcTransactionManager transactionManager) {
        return new JobBuilder("firstJob1", jobRepository)
                .start(step(jobRepository,transactionManager))
                .build();
    }

    private Step step(JobRepository jobRepository, JdbcTransactionManager transactionManager) {
        return new StepBuilder("step2", jobRepository)
                .tasklet((contribution, chunkContext) -> {
                    System.out.println(LocalDateTime.now());
                    System.out.println(Thread.currentThread().getName()+" 执行步骤....");
                    return RepeatStatus.FINISHED;
                },transactionManager).build();
    }
}
